package com.dtw.ditouwang.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.UserBean;
import com.dtw.ditouwang.beans.WechatUserBean;
import com.dtw.ditouwang.databinding.FragmentMineBinding;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseFragment;
import com.dtw.ditouwang.ui.coin.ProductListActivtiy;
import com.dtw.ditouwang.ui.login.LoginActivity;
import com.dtw.ditouwang.ui.main.MainActivity;
import com.dtw.ditouwang.utils.SharedPreferencesUtil;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MineFragment extends BaseFragment {
    FragmentMineBinding fragmentMineBinding;
    UserBean.DataBean userBean;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        userBean = UserRepository.getInstance().getUserBean().getData();
        fragmentMineBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_mine, container, false);
        fragmentMineBinding.setView(this);
        return fragmentMineBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fragmentMineBinding.setUser(userBean);
    }

    @Override
    public void onStart() {
        super.onStart();
        if (sharedPreferencesUtil.getLoginType() == SharedPreferencesUtil.LoginType.Phone) {
            webApi.login(0, sharedPreferencesUtil.getUserName(), sharedPreferencesUtil.getPassword(), null).enqueue(new Callback<UserBean>() {
                @Override
                public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                    UserRepository.getInstance().setUserBean(response.body());
                    sharedPreferencesUtil.setBean(response.body());
                    fragmentMineBinding.setUser(response.body().getData());
                }

                @Override
                public void onFailure(Call<UserBean> call, Throwable t) {

                }
            });
        } else {
            webApi.login(2, sharedPreferencesUtil.getUserName(), null, null).enqueue(new Callback<UserBean>() {
                @Override
                public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                    UserRepository.getInstance().setUserBean(response.body());
                    sharedPreferencesUtil.setBean(response.body());
                }

                @Override
                public void onFailure(Call<UserBean> call, Throwable t) {

                }
            });
        }
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.text_movement_report:
                startActivity(new Intent(getContext(), RecordActivity.class));
                break;
            case R.id.text_transcript:
                startActivity(new Intent(getContext(), RankingActivity.class));
                break;
            case R.id.text_report:
                startActivity(new Intent(getContext(), ReportActivity.class));
                break;
            case R.id.text_medal:
                startActivity(new Intent(getContext(), MedalActivity.class));
                break;
            case R.id.text_notify:
                startActivity(new Intent(getContext(), NotifyActivity.class));
                break;
            case R.id.text_security:
                startActivity(new Intent(getContext(), SecurityActivity.class));
                break;
            case R.id.text_setting:
                startActivity(new Intent(getContext(), SettingActivity.class));
                break;
            case R.id.view_user:
                Intent collection = new Intent(getContext(), MyCollectionActivity.class);
                collection.putExtra(MyCollectionActivity.KeyUserID, userBean.getId());
                startActivity(collection);
                break;
            case R.id.text_gold:
                Intent goldIntent = new Intent(getContext(), AssetsActivty.class);
                goldIntent.putExtra(AssetsActivty.KeyType, 1);
                startActivity(goldIntent);
                break;
            case R.id.text_energy:
                Intent energyIntent = new Intent(getContext(), AssetsActivty.class);
                energyIntent.putExtra(AssetsActivty.KeyType, 2);
                startActivity(energyIntent);
                break;
            case R.id.image_head:
                startActivity(new Intent(getContext(), UserInfoActivity.class));
                break;
            case R.id.text_coin:
                startActivity(new Intent(getContext(), ProductListActivtiy.class));
                break;
        }
    }
}
